package com.nutstudio.feitoys;

import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nutstudio.feitoys.Test.Test;
import com.nutstudio.feitoys.base.BaseActivity;
import com.nutstudio.feitoys.bean.DayWeatherBean;
import com.nutstudio.feitoys.utils.OkHttpManager;
import com.nutstudio.feitoys.utils.PreferenceUtils;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

//http://m.weather.com.cn/manage/citylist/provshi.html?id=10101
//热门城市
//http://m.weather.com.cn/manage/city1.html
//全国省
//http://m.weather.com.cn/manage/citylist/city3jdata/china.html
//市
//http://m.weather.com.cn/manage/citylist/city3jdata/provshi/10129.html
//区
//http://m.weather.com.cn/manage/citylist/city3jdata/station/1012910.html
public class MainActivity extends BaseActivity {

    @InjectView(R.id.wave_view)
    WaveView waveView;
    @InjectView(R.id.day1_title_tv)
    TextView day1TitleTv;
    @InjectView(R.id.day1_img_im)
    ImageView day1ImgIm;
    @InjectView(R.id.day1_des_tv)
    TextView day1DesTv;
    @InjectView(R.id.day2_title_tv)
    TextView day2TitleTv;
    @InjectView(R.id.day2_img_im)
    ImageView day2ImgIm;
    @InjectView(R.id.day2_des_tv)
    TextView day2DesTv;
    @InjectView(R.id.day3_title_tv)
    TextView day3TitleTv;
    @InjectView(R.id.day3_img_im)
    ImageView day3ImgIm;
    @InjectView(R.id.day3_des_tv)
    TextView day3DesTv;
    @InjectView(R.id.day4_title_tv)
    TextView day4TitleTv;
    @InjectView(R.id.day4_img_im)
    ImageView day4ImgIm;
    @InjectView(R.id.day4_des_tv)
    TextView day4DesTv;
    @InjectView(R.id.day5_title_tv)
    TextView day5TitleTv;
    @InjectView(R.id.day5_img_im)
    ImageView day5ImgIm;
    @InjectView(R.id.day5_des_tv)
    TextView day5DesTv;
    @InjectView(R.id.up_temp_line_view)
    MyLineView upTempLineView;
    @InjectView(R.id.down_temp_line_view)
    MyLineView downTempLineView;
    @InjectView(R.id.top_weather_status_tv)
    TextView topWeatherStatusTv;
    @InjectView(R.id.top_temp_tv)
    TextView topTempTv;
    @InjectView(R.id.top_air_tv)
    TextView topAirTv;
    @InjectView(R.id.top_wind_tv)
    TextView topWindTv;
    @InjectView(R.id.city_tv)
    TextView cityTv;
    @InjectView(R.id.menu_im)
    ImageView menuIm;
    private WaveHelper mWaveHelper;
    private int mBorderColor = Color.parseColor("#44FFFFFF");
    private List<DayWeatherBean> dayWeatherList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        ButterKnife.inject(this);
        getData();
        waveView.setBorder(0, mBorderColor);
        mWaveHelper = new WaveHelper(waveView);

        waveView.setShapeType(WaveView.ShapeType.SQUARE);
        waveView.setWaveColor(
                Color.parseColor("#FFFFFF"),
                WaveView.DEFAULT_FRONT_WAVE_COLOR);
        new Test().abc();
    }

    @Override
    protected void onResume() {
        super.onResume();
        waveView.setVisibility(View.VISIBLE);
        mWaveHelper.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWaveHelper.cancel();
    }

    private void formatData(String string) {
        Document doc = Jsoup.parse(string);
        // Log.d("mytag",doc.body().select("div.days7").select("img").attr("alt"));
        Log.d("mytag", doc.body().select("div.days7").select("li").toString());
        Elements links1 = doc.body().select("div.days7").select("li");
        for (Element link : links1) {
            DayWeatherBean bean = new DayWeatherBean();
            bean.setName(link.select("b").text());
            Elements weatherStatus = link.select("img");
            for (Element weather : weatherStatus) {
                bean.setStatus1(weatherStatus.get(0).select("img").attr("alt"));
                bean.setStatus1ImgUrl(weatherStatus.get(0).select("img").attr("src"));
                bean.setStatus2(weatherStatus.get(1).select("img").attr("alt"));
                bean.setStatus2ImgUrl(weatherStatus.get(1).select("img").attr("src"));
                Log.d("mytag", weather.toString());
            }
            String temp = link.select("span").text();
            int index = temp.indexOf("/");
            bean.setTemUp(temp.substring(0, index));
            bean.setTemDown(temp.substring(index + 1, temp.length()));
            dayWeatherList.add(bean);
            Log.d("mytag", bean.getName() + ">" + bean.getStatus1() + ">" + bean.getStatus2() + ">" + bean.getTemUp() + ">" + bean.getTemDown());
        }
        setDaysView();
        // Log.d("mytag",doc.body().getElementById("layout").select("script").toString());
        Elements links = doc.body().getElementById("layout").select("script");
        for (Element link : links) {
            if (link.toString().contains("dataSK")) {
                Log.d("mytag", link.toString());
                Log.d("mytag", link.toString().indexOf("dataSK = ") + ">>" + link.toString().indexOf("document"));
                Log.d("mytag", link.toString().substring(link.toString().indexOf("{"), link.toString().indexOf("}") + 1));
                try {
                    final JSONObject object = new JSONObject(link.toString().substring(link.toString().indexOf("{"), link.toString().indexOf("}") + 1));

                    try {
                        topWeatherStatusTv.setText(object.getString("weather"));
                        topAirTv.setText("PM2.5：" + object.getString("aqi_pm25"));
                        topWindTv.setText(object.getString("WD") + object.getString("WS"));
                        topTempTv.setText(object.getString("temp") + "°");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    Log.d("mytag", object.getString("cityname"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void setLocalData() {
        if (PreferenceUtils.getPrefString(MainActivity.this, "file", "localweather").length() > 0) {
            formatData(PreferenceUtils.getPrefString(MainActivity.this, "file", "localweather"));
        }
    }

    private void getData() {
        setLocalData();
        OkHttpManager.getAsyn("http://m.weather.com.cn/mweather/101110101.shtml", new OkHttpManager.ResultCallback() {
            @Override
            public void onError(Exception e) {

            }

            @Override
            public void onResponse(String string) {
                PreferenceUtils.setPrefString(MainActivity.this, "file", "localweather", string);
                formatData(string);
            }
        });

    }

    private void setDaysView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (dayWeatherList.size() > 5) {
                    int[] upList = new int[5];
                    int[] downList = new int[5];
                    for (int i = 0; i < 5; i++) {
                        upList[i] = Integer.decode(dayWeatherList.get(i).getTemUp().substring(0, dayWeatherList.get(i).getTemUp().length() - 1));
                        downList[i] = Integer.decode(dayWeatherList.get(i).getTemDown().substring(0, dayWeatherList.get(i).getTemDown().length() - 1));
                        Log.d("num", Integer.decode(dayWeatherList.get(i).getTemUp().substring(0, dayWeatherList.get(i).getTemUp().length() - 1)) + "");
                    }
                    upTempLineView.setHData(upList);
                    downTempLineView.setHData(downList);
                    day1TitleTv.setText(dayWeatherList.get(0).getName());
                    Picasso.with(MainActivity.this).load(dayWeatherList.get(0).getStatus1ImgUrl()).into(day1ImgIm);
                    day1DesTv.setText(dayWeatherList.get(0).getStatus1());

                    day2TitleTv.setText(dayWeatherList.get(1).getName());
                    Picasso.with(MainActivity.this).load(dayWeatherList.get(1).getStatus1ImgUrl()).into(day2ImgIm);
                    day2DesTv.setText(dayWeatherList.get(1).getStatus1());

                    day3TitleTv.setText(dayWeatherList.get(2).getName());
                    Picasso.with(MainActivity.this).load(dayWeatherList.get(2).getStatus1ImgUrl()).into(day3ImgIm);
                    day3DesTv.setText(dayWeatherList.get(2).getStatus1());

                    day4TitleTv.setText(dayWeatherList.get(3).getName());
                    Picasso.with(MainActivity.this).load(dayWeatherList.get(3).getStatus1ImgUrl()).into(day4ImgIm);
                    day4DesTv.setText(dayWeatherList.get(3).getStatus1());

                    day5TitleTv.setText(dayWeatherList.get(4).getName());
                    Picasso.with(MainActivity.this).load(dayWeatherList.get(4).getStatus1ImgUrl()).into(day5ImgIm);
                    day5DesTv.setText(dayWeatherList.get(4).getStatus1());
                }
            }
        });


    }

    @OnClick({R.id.city_tv, R.id.menu_im})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.city_tv:
                waveView.setVisibility(View.GONE);
                startActivity(new Intent(this, CityActivity.class), ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
                break;
            case R.id.menu_im:
                break;
        }
    }
}